#include "Sniper.h"
#include "RSManager.h"
#include "ShareComponents.h"
#include "Sprite.h"
#include "Box.h"
#include "BulletManager.h"
#include "GameDefine.h"
#define DELTA 40

Sniper::Sniper(Box* box) : Object(box,ObjEnemy_STATIC)
{
}


Sniper::~Sniper(void)
{
}

void Sniper::init()
{
	this->_left = RSManager::getInstance()->getSprite(SOLDIER2_LEFT);
	this->_right = RSManager::getInstance()->getSprite(SOLDIER2_RIGHT);
	this->_sprite = _left;	
	state = ENEMY_STATE::HIDE;
	_bulletCount = BULLET_COUNT;
	_shootTime = 0;	
	_delay = 0;
	_box->_size = _sprite->getRenderSize();
	_changeState = 0;
	_shootDirect = 3.14f;
	_hits = 1;
	_colBox->_v = _box->_v;
	_colBox->_size = _box->_size;
}

void Sniper::update()
{
	if(_hits <= 0) _isAlive = false;
	if((gl_player->getRight() > _box->getRight() + DELTA))
	{
		this->_sprite = _right;
		_shootDirect = 0.0f;
		_shootPosition.x = _box->getRight() - 5;//sai so
		_shootPosition.y = _box->getBottom() - 20;
	}

	if((gl_player->_position.x + DELTA < _box->_position.x ))
	{
		this->_sprite = _left;
		_shootDirect = 3.14f;
		_shootPosition.x = _box->_position.x;
		_shootPosition.y = _box->getBottom() - 15;
	}
	switch (state)
	{
	case ATTACK:
		_sprite->Reset(2);		
		_changeState += GAME_TIME;
		if(_changeState > 200)
		{			
			attack(GAME_TIME);
		}	
		break;
	case STAND_UP:
		_sprite->Reset(1);
		_changeState += GAME_TIME;
		if(_changeState > 80)
		{
			_changeState = 0;
			state = ATTACK;
		}			
		break;
	case SIT_DOWN:
		_sprite->Reset(1);
		_changeState += GAME_TIME;
		if(_changeState > 80)
		{
			_changeState = 0;
			state = HIDE;
		}	

		break;
	case HIDE:
		_sprite->Reset(0);
		_delay += GAME_TIME;
		if(_delay > ATTACK_TIME )
		{
			_delay = 0;
			state = STAND_UP;
		}
		break;
	}		
}

void Sniper::render()
{
	_sprite->Render(_box->_position);
}

void Sniper::attack(float time)
{
	if(state == ENEMY_STATE::ATTACK)
	{
		_shootTime += time;
		if(_shootTime > SHOOT_TIME)
		{
			_shootTime = 0;
			if(_bulletCount > 0)
			{
				_bulletCount--;
				BulletManager::getInstance()->shoot(B_E,this->_obj,_shootPosition,_shootDirect);				
			}
			else
			{
				_bulletCount--;
				if(_bulletCount <= -1)
				{
					_bulletCount = 1;
					state = SIT_DOWN;
					_changeState = 0;
				}								
			}
		}
	}
}

Box* Sniper::getCollisionBox()
{
	if(state == ATTACK)
	{		
		_colBox->_position = _box->_position;
		return _colBox;

	}
	return gl_BOX_0_0_0;
}
